"""
CPEM Daemon (cpemd.py)
Copyright 2010, C.J. Steele, all rights reserved.

C.J. Steele <coreyjsteele@gmail.com>

CPEM = "cross-platform extensible middleware" -- its a project that 
aims to provide a pluggable framework that allows for centralized 
systems management and configuration storage for mission critical 
applications.

XML RPC over HTTPS adapted from http://code.activestate.com/recipes/496786
"""
import sys,inspect,os
sys.path.append('lib')
from Registry import *
from Logger import *
from CPEMBaseServer import *
from plugins import init_plugins
from SecureXMLRPCServer import *
from SecureXMLRpcRequestHandler import *

#TODO: write a boot-strapper function that uses the key-based authentication to populate a memory-only SQLite3 database with our global configuration bits in it.
PATH = os.getcwd()
LISTEN_HOST = '0.0.0.0'
LISTEN_PORT = 3170
KEYFILE = 'certs/cert.pem'
CERTFILE = 'certs/cert.pem'
DBHOST = '127.0.0.1'
DBUSER = 'rcuser'
DBPASS = 'rcpass'
DBNAME = 'remoteControl'


def mainServer(HandlerClass=SecureXMLRpcRequestHandler,ServerClass=SecureXMLRPCServer):
	server_address = (LISTEN_HOST,LISTEN_PORT)
	CPEM_SERVER = SecureXMLRPCServer(server_address,HandlerClass,CERTFILE,KEYFILE)
	#CPEM_SERVER.register_instance(CPEMBaseServer())
	CPEM_SERVER.register_introspection_functions()

	# load our plugins -- this does a CPEM_SERVER.register_function() on each 
	# function found to be exported by our plugins (e.g. whatever is returned in 
	# the overload of __dir__, or functions in general)
	init_plugins(CPEM_SERVER)

	sa = CPEM_SERVER.socket.getsockname()
	print("Serving HTTPS on",sa[0],"port",sa[1])
	CPEM_SERVER.serve_forever()

if __name__ == '__main__':
	mainServer()
